import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import Xin from "../views/Xin.vue";
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    // 重新定向
    redirect: "/home/all",
    component: Home,
  },
  {
    path: "/home",
    name: "Home1",
    component: Home,
    children: [
      { path: "all", component: () => import("../components/All") },
      { path: "dan", component: () => import("../components/Dan") },
      { path: "hao", component: () => import("../components/Hao") }
    ]
  },
  {
    path: "/xin",
    name: "Xin",
    component: Xin,
  },
  {
    path: "/about",
    name: "About",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/About.vue"),
  },
];

const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  console.log(to, from);
  // 权限认证
  let token = localStorage.getItem('token');
  console.log(token);
  if (to.path === "/xin") {
    next()
  } else {
    if (token) {//有权限直接进行下一步
      next()
    } else {//没有权限去登录获得权限
      next({ name: "Xin" })
    }
  }
})

export default router;
